package com.ccjq.jq173.shosestore.web.controller.shoppingcart;

import com.ccjq.jq173.shosestore.member.po.User;
import com.ccjq.jq173.shosestore.product.service.impl.productServiceImpl;
import com.ccjq.jq173.shosestore.product.service.productService;
import com.ccjq.jq173.shosestore.shoppingcart.po.ShoppingCart;
import com.ccjq.jq173.shosestore.shoppingcart.service.ShoppingcartService;
import com.ccjq.jq173.shosestore.shoppingcart.service.impl.ShoppingcartServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class ShuLiangExistCheckServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         System.out.println("看看数量是否更新成功");
         boolean b=false;
         String quantity=request.getParameter("quantity");
         String ycshoppingid=request.getParameter("ycshoppingid");
         ShoppingcartService ss = new ShoppingcartServiceImpl();
         System.out.println("ycshoppingid="+ycshoppingid);
         //b =ss.update(ycshoppingid,Integer.parseInt(quantity));
         User user=(User)request.getSession().getAttribute("user");
        System.out.println(user.getUserId());
         ShoppingCart shoppingCart=ss.queryCar(ycshoppingid,user.getUserId());
         String productId=shoppingCart.getProductId();
         productService productService=new productServiceImpl();
         String count=productService.getStock(productId);
        System.out.println("count="+count+"==========quantity"+quantity);
         if(Integer.parseInt(count)>Integer.parseInt(quantity))
         {
             b =ss.update(ycshoppingid,Integer.parseInt(quantity));
         }
         String str="{\"code\":200,\"result\":"+b+"}";
         PrintWriter out=response.getWriter();
         out.write(str);
         out.flush();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doPost(request,response);
    }
}
